#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    player = new QMediaPlayer(this);
    // player->setNotifyInterval(2000);
    videoWidget = new QVideoWidget(this);
    videoWidget->setMinimumSize(640,320);
    audioOutput = new QAudioOutput();
    player->setVideoOutput(videoWidget);
    player->setAudioOutput(audioOutput);
    ui->verticalLayout->addWidget(videoWidget);

    // //改变播放状态
    connect(player, &QMediaPlayer::playbackStateChanged, this, &MainWindow::onstatechanged);
    connect(player, &QMediaPlayer::positionChanged, this, &MainWindow::onpositionchanged);
    // // // connect(player, &QMediaPlayer::positionChanged, [=](){
    // // //     ui->graphicsView->update();
    // // // });
    connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::ondurationChanged);

    //播放进度silder
    ui->playbackProgressSlider->setRange(0, this->durationTime);
    //拖动响应
    connect(ui->playbackProgressSlider, &QAbstractSlider::sliderMoved, [=](int v){
        player->setPosition(v);
    });

    //声音silder
    ui->soundProgressSlider->setRange(0, 100);
    //拖动 调整音量
    connect(ui->soundProgressSlider, &QAbstractSlider::valueChanged, [=](int v){
        // this->audioOutput, &QAudioOutput::setVolume
        //计算音量滑动条代表的线性音量值
        //qreal 是 Qt 框架中定义的一个数据类型，代表了一个浮点数（floating point number）。在大多数平台上，它等同于 C++ 的 double 类型，即双精度浮点数。
        qreal linearVolume = QAudio::convertVolume(ui->soundProgressSlider->value() / qreal(100),
                                                   QAudio::LogarithmicVolumeScale,
                                                   QAudio::LinearVolumeScale);
        this->audioOutput->setVolume(linearVolume);

        //优化 声音拖动到0时 声音图片切换
        if(v == 0){
            ui->soundBtn->setIcon(QIcon(":/img/shengyinguanbi.png"));
            this->soundBtn = false;
        }else{
            ui->soundBtn->setIcon(QIcon(":/img/shengyin-dakai.png"));
            this->soundBtn = true;
        }
    });


    //音乐播放页面
    music_player *music = new music_player;
    ui->tabWidget->addTab(music, "音乐视频播放器");


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onstatechanged(QMediaPlayer::PlaybackState newState)
{
    if(newState == QMediaPlayer::StoppedState){
        qDebug() << "播放器已停止";
    }else if(newState == QMediaPlayer::PlayingState){
        qDebug() << "开始播放";
    }else if(newState == QMediaPlayer::PausedState){
        qDebug() << "播放器已暂停";
    }else{
        //QMediaPlayer::BufferedMediaState
        qDebug() << "正在缓冲";
    }
}

//用于处理播放器媒体时长改变时的操作
void MainWindow::ondurationChanged(qint64 duratoin)
{
    // qDebug() << duratoin;
    durationTime = duratoin/1000;   //总时长
    ui->playbackProgressSlider->setMaximum(duratoin);
}

//用于处理播放器播放位置改变时的操作
void MainWindow::onpositionchanged(qint64 position)
{
    if(!ui->playbackProgressSlider->isSliderDown())
        ui->playbackProgressSlider->setValue(position);
    // qDebug() << "位置" << position;
}


void MainWindow::on_addVideoBtn_clicked()
{
    QString videoPath = QDir::homePath();   //获取系统当前目录
    QString dlgtitle = "请选择视频文件";   //打开文件对话框标题
    QString strfilter = "MP4 Files(*.mp4);;All files(*.8)"; //打开视频文件过滤器

    QString allfiles = QFileDialog::getOpenFileName(this, dlgtitle, videoPath, strfilter);
    qDebug() << allfiles;

    if(allfiles.isEmpty()){
        QMessageBox::critical(this, "错误", "打开视频文件失败，请重新检查？", QMessageBox::Yes);
        return ;
    }

    QFileInfo fileinformation(allfiles);    //获取文件信息
    ui->fileNameLabel->setText(fileinformation.fileName());

    // durationTime = player->metaData(QMediaMetaData::Duration)->toInt();
    // player->setMedia(QMediaContent(QUrl(allfiles)));
    player->setSource(QUrl::fromLocalFile(allfiles));
    // player->setVolume(35);
    player->play();

}


void MainWindow::on_playBtn_clicked()
{
    if(player != NULL){
        player->play();
    }else{
        qDebug() << "QMediaPlayer未初始化";
    }
}


void MainWindow::on_pauseBtn_clicked()
{
    if(player != NULL){
        player->pause();
    }else{
        qDebug() << "QMediaPlayer未初始化";
    }
}


void MainWindow::on_stopBtn_clicked()
{
    if(player != NULL){
        player->stop();
    }else{
        qDebug() << "QMediaPlayer未初始化";
    }
}


void MainWindow::on_soundBtn_clicked()
{
    if(this->soundBtn){
        ui->soundBtn->setIcon(QIcon(":/img/shengyinguanbi.png"));
        ui->soundProgressSlider->setValue(0);
        if(player != NULL) audioOutput->setVolume(0);
        this->soundBtn = false;
    }else{
        ui->soundBtn->setIcon(QIcon(":/img/shengyin-dakai.png"));
        ui->soundProgressSlider->setValue(35);
        if(player != NULL) audioOutput->setVolume(35);
        this->soundBtn = true;
    }
}

